Quick Index

Overview


NPM (or npm) is a dependency management tool.

Fun (or not) side note: the authors keep it a secret as to what NPM stands for -- originally thought to be "Node Package Manager".

As beginners, we'll use npm for:


Common Commands


Notes


npm is installed as part of Node.js install (no additional work needed)
Use switch -v to get the installed version of npm
>npm -v
7.6.3
Install latest version of npm.
>npm install -g npm@latest
or
>npm install -g npm
Initialize project via prompts.
>npm init
Install a public package using npm.

Where "http-status-codes" is an example public package.

Note that the installed module will be put in the dependencies list in "package.json".
>npm install http-status-codes
Use the "--save-dev" (or "-D") switch to install a package with a dev dependency.

This is for development-only packages.

Note that the installed module will be put in the devDependencies list in "package.json".
>npm install --save-dev foo
or
>npm install -D foo
Use the "-g" switch to install a package globally.

Note: With Node.js it is suggested to rarely install global packages.
>npm install -g foo
Note: When archiving, submitting, delivering our code we should first delete the "node_modules" directory.

To rebuild it we simply do the vanilla "install" command as shown.

This will install all dependencies from "package.json".
>npm install
We simply add the "--production" flag when installing dependencies in production.

This will install with dependencies and without devDependencies (from "package.json").
>npm install --production
The command shown will initialize without prompts (accepting all defaults).

We might do this for scratch (throw-away) projects.
>npm -y init
If you want to do a simple test that npm is working, or your are troubleshooting, here is a short npm sanity check.


Setting Project Defaults


We can set npm project defaults (used by npm "init") to make setup easier.

Set Default Configuration Items
Let's open up a console (anywhere on your computer) and set the email and name (customize as you like).
>npm set init.author.email "foo@abc.com"
>npm set init.author.name "Foo"


npm links


More info on npm links is here...

Module vs Package


As beginners, we'll initially focus on modules (js files with exports).

Later we'll learn more about the fact that any file or directory that can be loaded via "require()" is a module. This includes JS files, and also includes directories setup to be modules: 1) with "package.json" and containing a "main" field); 2) Directory with "index.js"

And we'll also learn that modules that have a "package.json" file are also packages.

References